///*
// * chunk.c
// *
// *  Created on: Apr 29, 2011
// *      Author: redchen
// */
//
//#include <emalloc.h>
//#include <stdutils.h>
//#include <logging.h>
//#include <file.h>
//#include <io.h>
//#include <assert.h>
//#include <strings.h>
//#include <string.h>
//#include "iterator_utils.h"
//
//#include "chunk.h"
//
//#define MAGIC_LENGTH 8
//
//#define MAX_INDEX_SIZE 255
//
//#define TRAILER_MAGIC "trailer:"
//#define CHUNK_INDEX_MAGIC "index..:"
//
//typedef struct
//{
//    int32_t size;
//    int32_t offset; //beginning of the segment in chunk
//    int64_t timestamp;
//} SegmentIndex;
//
//typedef struct
//{
//    int32_t size;
//    char* data;
//} Segment;
//
//typedef struct
//{
//    char magic[MAGIC_LENGTH];
//    int32_t segment_num;
//    List* segment_indexs; //List<SegmentIndex>
//} ChunkIndex;
//
///**
// * Describe the whole chunk of information
// */
//typedef struct
//{
//    char magic[MAGIC_LENGTH];
//    char filename[NAMESPACE_LEN];
//    /**
//     * _chunk_status{
//     *     SERVICE,
//     *    DELETED
//     *  };
//     */
//    int32_t status; //is deleted
//    int32_t allocated_size;
//    int32_t used_size;
//    int32_t end_segment_offset;
//    int32_t chunk_index_offset;
//    int32_t trailer_offset; //need to stored to end of the file
//    int64_t start_timestamp;
//    int64_t end_timestamp;
//} Trailer;
//
//struct _Chunk_
//{
//    int64_t id; //chunk handle
//    char path[NAMESPACE_LEN];
//    Trailer* trailer;
//    ChunkIndex* chunk_index;
//    pthread_mutex_t mutex_lock; //mutex lock
//    pthread_rwlock_t rw_lock; //read and writer lock
//};
//
//static Logger* mLog = NULL;
//
//static ChunkIndex* create_chunk_index()
//{
//    ChunkIndex* chunk_index = emalloc(sizeof(ChunkIndex));
//    strcpy(chunk_index->magic, CHUNK_INDEX_MAGIC);
//    chunk_index->segment_num = 0;
//    chunk_index->segment_indexs = list_new();
//    return chunk_index;
//}
//
//static int32_t get_chunk_index_size(ChunkIndex* chunk_index)
//{
//    int32_t size = 0, i = 0;
//    size += sizeof(chunk_index->magic);
//    size += sizeof(chunk_index->segment_num);
//    for (; i < list_size(chunk_index->segment_indexs); i++)
//    {
//        size += sizeof(SegmentIndex);
//    }
//    return size;
//}
//
//static Trailer* create_trailer(char* filename, int32_t status, int32_t end_segment_offset, int32_t begin_chunk_index_offset, int32_t used_size, int32_t allocated_size, int32_t trailer_offset, int64_t start_timestamp, int64_t end_timestamp)
//{
//    Trailer* trailer = emalloc(sizeof(Trailer));
//    strcpy(trailer->magic, TRAILER_MAGIC);
//    strcpy(trailer->filename, filename);
//    trailer->status = status;
//    trailer->used_size = used_size;
//    trailer->allocated_size = allocated_size;
//    trailer->end_segment_offset = end_segment_offset;
//    trailer->chunk_index_offset = begin_chunk_index_offset;
//    trailer->trailer_offset = trailer_offset;
//    trailer->start_timestamp = start_timestamp;
//    trailer->end_timestamp = end_timestamp;
//    return trailer;
//}
//
///**
// *    init the chunk struct ,not create new local chunk
// */
//Chunk* chunk_init(const char* path, int64_t id, char* filename, int32_t size)
//{
//    mLog = GET_LOGGER("/lfs/storage");
//    Chunk* chunk = emalloc(sizeof(Chunk));
//    chunk->id = id;
//    strcpy(chunk->path, path);
//    chunk->chunk_index = create_chunk_index();
//
//    int32_t chunk_index_size = get_chunk_index_size(chunk->chunk_index);
//    int32_t trailer_size = sizeof(Trailer);
//
//    int32_t begin_chunk_index_offset = size - trailer_size - chunk_index_size;
//    int32_t used_size = trailer_size + chunk_index_size;
//    int32_t trailer_offset = size - trailer_size;
//
//    Trailer* tralier = create_trailer(filename, SERVICE, 0, begin_chunk_index_offset, used_size, size, trailer_offset, 0, 0);
//    chunk->trailer = tralier;
//    pthread_mutex_init(&chunk->mutex_lock, NULL);
//    pthread_rwlock_init(&chunk->rw_lock, NULL);
//    return chunk;
//}
//
//static void flush_segment_index(FILE* fp, SegmentIndex* index)
//{
//    fwrite(index, sizeof(SegmentIndex), 1, fp);
//}
//
//static SegmentIndex* load_segment_index(FILE* fp)
//{
//    SegmentIndex* index = emalloc(sizeof(SegmentIndex));
//    fread(index, sizeof(SegmentIndex), 1, fp);
//    return index;
//}
//
//static void flush_chunk_index(FILE* fp, ChunkIndex* chunk_index)
//{
//    fwrite(chunk_index->magic, sizeof(chunk_index->magic), 1, fp);
//    fwrite(&chunk_index->segment_num, sizeof(chunk_index->segment_num), 1, fp);
//    iterator_list(chunk_index->segment_indexs)
//    {
//        flush_segment_index(fp, list_next(chunk_index->segment_indexs));
//    }
//}
//
//static ChunkIndex* load_chunk_index(FILE* fp)
//{
//    ChunkIndex* chunk_index = emalloc(sizeof(ChunkIndex));
//    fread(chunk_index->magic, sizeof(chunk_index->magic), 1, fp);
//    fread(&chunk_index->segment_num, sizeof(chunk_index->segment_num), 1, fp);
//    chunk_index->segment_indexs = list_new();
//    int32_t i = 0;
//    for (; i < chunk_index->segment_num; i++)
//    {
//        list_push_back(chunk_index->segment_indexs, load_segment_index(fp));
//    }
//    return chunk_index;
//}
//
//static void flush_trailer(FILE* fp, Trailer* trailer)
//{
//    fwrite(trailer, sizeof(Trailer), 1, fp);
//}
//
//static Trailer* load_trailer(FILE* fp)
//{
//    Trailer* trailer = emalloc(sizeof(Trailer));
//    memset(trailer->filename, 0, sizeof(trailer->filename));
//    fread(trailer, sizeof(Trailer), 1, fp);
//    return trailer;
//}
//
//static void* segment_destory(Segment* seg)
//{
//    if (seg != NULL)
//    {
//        if (seg->data != NULL)
//        {
//            efree(seg->data);
//        }
//        efree(seg);
//    }
//    return NULL;
//}
//
///**
// * init the chunk struct and create new chunk
// * path:Tablet
// */
//Chunk* chunk_create(const char* path, int64_t id, char* filename, int32_t size)
//{
//    char chunk_name[512];
//#if __WORDSIZE == 64
//    sprintf(chunk_name, "%s/%s%ld", path, CHUNK_PREFIX, id);
//#else
//    sprintf(chunk_name, "%s/%s%lld", path, CHUNK_PREFIX, id);
//#endif
//    if (io_file_exist(chunk_name))
//    {
//        logger(FATAL(mLog), "chunk '%s' is exist,can not create the same name chunk", chunk_name);
//        return NULL;
//    }
//    Chunk* chunk = chunk_init(path, id, filename, size);
//    FILE *fp = NULL;
//    if ((fp = fopen(chunk_name, "wb")) == NULL)
//    {
//        logger(WARN(mLog), "can not create the file [%s]", chunk_name);
//        return NULL;
//    }
//
//    fseek(fp, chunk->trailer->chunk_index_offset, SEEK_SET);
//    flush_chunk_index(fp, chunk->chunk_index);
//
//    int32_t trailer_offset = chunk->trailer->trailer_offset;
//    fseek(fp, trailer_offset, SEEK_SET);
//
//    flush_trailer(fp, chunk->trailer);
//
//    fclose(fp);
//    return chunk;
//}
//
//Chunk* chunk_load(const char* path, int64_t id)
//{
//    FILE* fp = NULL;
//    char chunk_name[512];
//#if __WORDSIZE == 64
//    sprintf(chunk_name, "%s/%s%ld", path, CHUNK_PREFIX, id);
//#else
//    sprintf(chunk_name, "%s/%s%lld", path, CHUNK_PREFIX, id);
//#endif
//    if ((fp = fopen(chunk_name, "rb")) == NULL)
//    {
//        logger(WARN(mLog), "can not find the file [%s]", chunk_name);
//        return NULL;
//    }
//
//    int32_t trailer_size = sizeof(Trailer);
//    fseek(fp, -trailer_size, SEEK_END);
//    logger(DEBUG(mLog), "trailer_size:%d", trailer_size);
//    //printf("%ld\n",ftell(fp));
//    Trailer* trailer = load_trailer(fp);
//
//    fseek(fp, trailer->chunk_index_offset, SEEK_SET);
//    ChunkIndex* chunk_index = load_chunk_index(fp);
//    fclose(fp);
//
//    //assign
//    Chunk* chunk = emalloc(sizeof(Chunk));
//    chunk->id = id;
//    strcpy(chunk->path, path);
//    chunk->chunk_index = chunk_index;
//    chunk->trailer = trailer;
//    pthread_mutex_init(&chunk->mutex_lock, NULL);
//    pthread_rwlock_init(&chunk->rw_lock, NULL);
//    return chunk;
//}
//
//Chunk* chunk_load_by_name_space(const char* path, char* chunk_name)
//{
//    chunk_name += 2;
//    int64_t id = atoll(chunk_name);
//    return chunk_load(path, id);
//}
//
//int chunk_match(void* chunk1, void* chunk2)
//{
//    Chunk* c1 = (Chunk*) chunk1;
//    Chunk* c2 = (Chunk*) chunk2;
//    if (c1->id == c2->id)
//    {
//        return 0;
//    }
//    else
//    {
//        return -1;
//    }
//}
//
//Chunk* chunk_get_by_id(List* chunk_list, int64_t id)
//{
//    if (NULL == chunk_list)
//    {
//        return NULL;
//    }
//
//    Chunk* chunk = NULL;
//    iterator_list(chunk_list)
//    {
//        Chunk* tmp = list_next(chunk_list);
//        if (tmp->id == id)
//            chunk = tmp;
//    }
//    return chunk;
//}
//
//static Segment* create_segment(char* data, int32_t size)
//{
//    Segment* segment = emalloc(sizeof(Segment) + size);
//    segment->size = size;
//    segment->data = emalloc(size);
//    memcpy(segment->data, data, size);
//    return segment;
//}
//
//static void* segment_free(Segment* seg)
//{
//    if (seg != NULL)
//    {
//        if (seg->data != NULL)
//        {
//            seg->data = efree(seg->data);
//        }
//        seg = efree(seg);
//    }
//    return NULL;
//}
//
//static void flush_segment(FILE* fp, int32_t offset, Segment* segment)
//{
//    fseek(fp, offset, SEEK_SET);
//    //fwrite(&segment->size, sizeof(segment->size), 1, fp);
//    fwrite(segment->data, segment->size, 1, fp);
//}
//
//boolean chunk_append(Chunk* chunk, SFile* file)
//{
//    int32_t free_size = chunk_get_free_size(chunk);
//    if (free_size < file->size)
//    {
//        return false;
//    }
//
//    pthread_rwlock_wrlock(&chunk->rw_lock);
//
//    Trailer* trailer = chunk->trailer;
//    ChunkIndex* chunk_index = chunk->chunk_index;
//    Segment* segment = create_segment(file->data, file->size);
//
//    char chunk_name[512];
//#if __WORDSIZE == 64
//    sprintf(chunk_name, "%s/%s%ld", chunk->path, CHUNK_PREFIX, chunk->id);
//#else
//    sprintf(chunk_name, "%s/%s%lld", chunk->path, CHUNK_PREFIX, chunk->id);
//#endif
//    FILE* fp = NULL;
//    if ((fp = fopen(chunk_name, "rb+")) == NULL)
//    {
//        segment = segment_free(segment);
//        return false;
//    }
//
//    flush_segment(fp, trailer->end_segment_offset, segment);
//
//    //update chunk index
//    SegmentIndex* index = emalloc(sizeof(SegmentIndex));
//    //query all this file and get number
//    index->size = segment->size;
//    index->offset = trailer->end_segment_offset;
//    index->timestamp = file->timestamp;
//    list_push_back(chunk_index->segment_indexs, index);
//    chunk_index->segment_num++;
//
//    //update trailer
//    trailer->used_size += segment->size + sizeof(SegmentIndex);
//    trailer->chunk_index_offset -= sizeof(SegmentIndex);
//    trailer->end_segment_offset += segment->size;
//
//    //update timestamp
//    if (trailer->start_timestamp == 0)
//    {
//        trailer->start_timestamp = file->timestamp;
//    }
//    if (trailer->end_timestamp == 0)
//    {
//        trailer->end_timestamp = file->timestamp;
//    }
//    if (trailer->start_timestamp > file->timestamp)
//    {
//        trailer->start_timestamp = file->timestamp;
//    }
//    if (trailer->end_timestamp < file->timestamp)
//    {
//        trailer->end_timestamp = file->timestamp;
//    }
//
//    //write to disk
//    fseek(fp, trailer->chunk_index_offset, SEEK_SET);
//    flush_chunk_index(fp, chunk->chunk_index);
//    fseek(fp, trailer->trailer_offset, SEEK_SET);
//    flush_trailer(fp, chunk->trailer);
//
//    fclose(fp);
//
//    pthread_rwlock_unlock(&chunk->rw_lock);
//
//    //free
//    segment = segment_free(segment);
//    return true;
//}
//
//int32_t file_get_free_size(Chunk* chunk)
//{
//    int32_t free_size = chunk->trailer->allocated_size - chunk->trailer->used_size;
//    return free_size;
//}
//
//int32_t chunk_get_extra_index_size(int32_t times)
//{
//    return times * sizeof(SegmentIndex);
//}
//
//int64_t chunk_get_latest_timestamp(Chunk* chunk)
//{
//    return chunk->trailer->end_timestamp;
//}
//
//static Segment* get_segement(FILE* fp, SegmentIndex* si)
//{
//    Segment* segment = emalloc(sizeof(Segment));
//    segment->size = si->size;
//    segment->data = emalloc(si->size);
//    fseek(fp, si->offset, SEEK_SET);
//    fread(segment->data, si->size, 1, fp);
//    return segment;
//}
//
//static SegmentIndex* get_segment_index_by_timestamp(List* segment_index_list, int64_t timestamp)
//{
//    if (segment_index_list == NULL || timestamp == 0 || list_size(segment_index_list) == 0)
//    {
//        return NULL;
//    }
//    List* tmp = segment_index_list;
//    SegmentIndex* index = NULL;
//    iterator_list(tmp)
//    {
//        index = list_next(tmp);
//        if (index->timestamp == timestamp)
//        {
//            return index;
//        }
//    }
//    return NULL;
//}
//
///**
// * return List<SegmentIndex>
// */
//static int32_t get_segment_index_by_timestamp_start_end(List* segment_index_list, int64_t start, int64_t end, List* si_list)
//{
//    List* tmp = segment_index_list;
//    SegmentIndex* index = NULL;
//    iterator_list(tmp)
//    {
//        index = list_next(tmp);
//        if (index->timestamp >= start && index->timestamp <= end)
//        {
//            list_push_front(si_list, index);
//        }
//    }
//    return 1;
//}
//
//Status chunk_get_sfile_by_timestamp(Chunk* chunk, int64_t timestamp,/*out*/SFile* sfile)
//{
//    SegmentIndex* si = get_segment_index_by_timestamp(chunk->chunk_index->segment_indexs, timestamp);
//    if (si == NULL)
//    {
//        logger(INFO, "SegmentIndex is NULL");
//        return LFS_SUCC_NULL;
//    }
//
//    pthread_rwlock_rdlock(&chunk->rw_lock);
//
//    //open file
//    FILE* fp = NULL;
//    char chunk_name[512];
//#if __WORDSIZE == 64
//    sprintf(chunk_name, "%s/%s%ld", chunk->path, CHUNK_PREFIX, chunk->id);
//#else
//    sprintf(chunk_name, "%s/%s%lld", chunk->path, CHUNK_PREFIX, chunk->id);
//#endif
//    if ((fp = fopen(chunk_name, "rb")) == NULL)
//    {
//        return LFS_FAIL;
//    }
//
//    Segment* segment = get_segement(fp, si);
//    //set value
//    strcpy(sfile->filename, chunk->trailer->filename);
//    sfile->timestamp = si->timestamp;
//    sfile->data = segment->data;
//    sfile->size = segment->size;
//
//    fclose(fp);
//
//    pthread_rwlock_unlock(&chunk->rw_lock);
//
//    efree(segment);
//
//    return LFS_SUCC;
//}
//
///**
// * return List<SFile>
// */
//Status chunk_get_sfiles_by_timestamp_start(Chunk* chunk, int64_t start,/*out*/List* sfiles)
//{
//    int64_t end = chunk_get_latest_timestamp(chunk);
//    return chunk_get_sfiles_by_timestamp_start_end(chunk, start, end, sfiles);
//}
//
///**
// * return List<SFile>
// */
//Status chunk_get_sfiles_by_timestamp_start_end(Chunk* chunk, int64_t start, int64_t end,/*out*/List* sfile_list)
//{
//    logger(DEBUG(mLog), "Begin chunk_get_sfiles_by_timestamp_start_end");
//    List* si_list = list_new();
//    get_segment_index_by_timestamp_start_end(chunk->chunk_index->segment_indexs, start, end, si_list);
//
//    if (list_size(si_list) == 0)
//    {
//        list_free(si_list);
//        return LFS_SUCC_NULL;
//    }
//
//    pthread_rwlock_rdlock(&chunk->rw_lock);
//
//    //open file
//    FILE* fp = NULL;
//    char chunk_name[512];
//#if __WORDSIZE == 64
//    sprintf(chunk_name, "%s/%s%ld", chunk->path, CHUNK_PREFIX, chunk->id);
//#else
//    sprintf(chunk_name, "%s/%s%lld", chunk->path, CHUNK_PREFIX, chunk->id);
//#endif
//    if ((fp = fopen(chunk_name, "rb")) == NULL)
//    {
//        sleep(1);
//        abort();
//    }
//
//    SegmentIndex* si = NULL;
//    iterator_list(si_list)
//    {
//        si = list_next(si_list);
//        Segment* segment = get_segement(fp, si);
//        SFile* file = sfile_create(chunk->trailer->filename, segment->data, segment->size, si->timestamp);
//        list_push_front(sfile_list, file);
//        segment_destory(segment);
//    }
//
//    fclose(fp);
//
//    pthread_rwlock_unlock(&chunk->rw_lock);
//    list_free(si_list);
//    return LFS_SUCC;
//}
//
//static List* chunk_get_sfiles_info_by_filename(Chunk* chunk, char* filename)
//{
//    List* list = list_new();
//    if (string_equals(chunk->trailer->filename, filename) == 0)
//    {
//        ChunkIndex* ci = chunk->chunk_index;
//        SegmentIndex* index = NULL;
//        iterator_list(ci->segment_indexs)
//        {
//            index = list_next(ci->segment_indexs);
//            SFileInfo* info = emalloc(sizeof(SFileInfo));
//            strcpy(info->filename, filename);
//            info->size = index->size;
//            info->timestamp = index->timestamp;
//            list_push_front(list, info);
//        }
//    }
//    return list;
//}
//
//List* chunk_get_all_sfiles_info_by_filename(List* chunk_list, char* filename, int64_t id)
//{
//    List* list = NULL;
//    Chunk* ch = NULL;
//    iterator_list(chunk_list)
//    {
//        ch = list_next(chunk_list);
//        if (string_equals(ch->trailer->filename, filename) == 0 && ch->id == id)
//        {
//            List* tmp = chunk_get_sfiles_info_by_filename(ch, filename);
//            list = tmp;
//        }
//    }
//    return list;
//}
//
//char* chunk_get_filename(Chunk* chunk)
//{
//    return chunk->trailer->filename;
//}
//
//int64_t chunk_get_id(Chunk* chunk)
//{
//    return chunk->id;
//}
//
//static void segment_index_free(void* target)
//{
//    SegmentIndex* si = target;
//    efree(si);
//}
//
//static void* chunk_index_destroy(ChunkIndex* index)
//{
//    logger(DEBUG(mLog), "destroy chunk index");
//    if (index != NULL)
//    {
//        if (index->segment_indexs != NULL)
//        {
//            list_destory(index->segment_indexs, segment_index_free);
//        }
//        free(index);
//    }
//    return NULL;
//}
//
//void chunk_fill_full(Chunk* chunk)
//{
//    pthread_rwlock_wrlock(&chunk->rw_lock);
//    chunk->trailer->used_size = chunk->trailer->allocated_size;
//    pthread_rwlock_unlock(&chunk->rw_lock);
//}
//
//void chunk_index_free(void* target)
//{
//    ChunkIndex* ci = target;
//    if (ci != NULL)
//    {
//        efree(ci->segment_indexs);
//        efree(ci);
//    }
//}
//
//void chunk_free(void* target)
//{
//    Chunk* chunk = target;
//    if (chunk != NULL)
//    {
//        efree(chunk->chunk_index);
//        efree(chunk->trailer);
//        pthread_mutex_destroy(&chunk->mutex_lock);
//        pthread_rwlock_destroy(&chunk->rw_lock);
//        efree(chunk);
//    }
//}
//
///**
// * free and destory
// */
//
//void* chunk_destroy(Chunk* chunk)
//{
//    if (chunk != NULL)
//    {
//        if (chunk->chunk_index != NULL)
//        {
//            chunk->chunk_index = chunk_index_destroy(chunk->chunk_index);
//        }
//        if (chunk->trailer != NULL)
//        {
//            efree(chunk->trailer);
//        }
//        pthread_mutex_destroy(&chunk->mutex_lock);
//        pthread_rwlock_destroy(&chunk->rw_lock);
//        efree(chunk);
//    }
//    return NULL;
//}
//
//int32_t chunk_get_status(Chunk* chunk)
//{
//    return chunk->trailer->status;
//}
//
//void chunk_set_status(Chunk* chunk, int32_t status)
//{
//    chunk->trailer->status = status;
//    char chunk_name[512];
//#if __WORDSIZE == 64
//    sprintf(chunk_name, "%s/%s%ld", chunk->path, CHUNK_PREFIX, chunk->id);
//#else
//    sprintf(chunk_name, "%s/%s%lld", chunk->path, CHUNK_PREFIX, chunk->id);
//#endif
//    FILE* fp = NULL;
//    if ((fp = fopen(chunk_name, "rb+")) == NULL)
//    {
//        return;
//    }
//    fseek(fp, chunk->trailer->trailer_offset, SEEK_SET);
//    flush_trailer(fp, chunk->trailer);
//    fclose(fp);
//}
//
//Status chunk_delete(Chunk* chunk, boolean is_instant)
//{
//    if (is_instant)
//    {
//        char buf[512];
//#if __WORDSIZE == 64
//        sprintf(buf,"%s/c.%ld",chunk->path,chunk->id);
//#else
//        sprintf(buf, "%s/c.%lld", chunk->path, chunk->id);
//#endif
//        remove(buf);
//        chunk_destroy(chunk);
//    }
//    else
//    {
//        chunk_set_status(chunk, DELETED);
//    }
//    return LFS_SUCC;
//}
//
//void chunk_show_trailer(Chunk* chunk)
//{
//    Trailer* trailer = chunk->trailer;
//    char magic_buf[9] =
//    { 0 };
//    strncpy(magic_buf, trailer->magic, 8);
//#if __WORDSIZE == 64
//    printf("id                :%ld\n",chunk->id);
//#else
//    printf("id                :%lld\n", chunk->id);
//#endif
//    printf("path              :%s\n", chunk->path);
//    printf("magic             :%s\n", magic_buf);
//    printf("filename          :%s\n", trailer->filename);
//    printf("status            :%d\n", trailer->status);
//    printf("trailer_size      :%d\n", (int32_t) sizeof(Trailer));
//    printf("used_size         :%d\n", trailer->used_size);
//    printf("allocated_size    :%d\n", trailer->allocated_size);
//    printf("end_segment_offset:%d\n", trailer->end_segment_offset);
//    printf("chunk_index_offset:%d\n", trailer->chunk_index_offset);
//    printf("trailer_offset    :%d\n", trailer->trailer_offset);
//#if __WORDSIZE == 64
//    printf("start_timestamp   :%ld\n",trailer->start_timestamp);
//#else
//    printf("start_timestamp   :%lld\n", trailer->start_timestamp);
//#endif
//#if __WORDSIZE == 64
//    printf("end_timestamp     :%ld\n",trailer->end_timestamp);
//#else
//    printf("end_timestamp     :%lld\n", trailer->end_timestamp);
//#endif
//}
//
//void chunk_show_data(Chunk* chunk)
//{
//    List* sfile_list = list_new();
//    List* si_list = list_new();
//    get_segment_index_by_timestamp_start_end(chunk->chunk_index->segment_indexs, 0, 9331449388101L, si_list);
//    FILE* fp = NULL;
//    char chunk_name[512];
//#if __WORDSIZE == 64
//    sprintf(chunk_name, "%s/%s%ld", chunk->path, CHUNK_PREFIX, chunk->id);
//#else
//    sprintf(chunk_name, "%s/%s%lld", chunk->path, CHUNK_PREFIX, chunk->id);
//#endif
//    if ((fp = fopen(chunk_name, "rb")) == NULL)
//    {
//        sleep(1);
//        abort();
//    }
//
//    SegmentIndex* si = NULL;
//    iterator_list(si_list)
//    {
//        si = list_next(si_list);
//        Segment* segment = get_segement(fp, si);
//        SFile* file = sfile_create(chunk->trailer->filename, segment->data, segment->size, si->timestamp);
//        list_push_front(sfile_list, file);
//        segment_destory(segment);
//    }
//
//    fclose(fp);
//    //show data
//    iterator_list(sfile_list)
//    {
//        SFile* file = list_next(sfile_list);
//#if __WORDSIZE == 64
//        printf("%ld : %s\n",file->timestamp,(char*)file->data);
//#else
//        printf("%lld : %s\n", file->timestamp, (char*) file->data);
//#endif
//    }
//    list_free(si_list);
//}
//
//Status chunk_get_all_data(Chunk* chunk, /*out*/Buffer* buf)
//{
//    //get lock
//    Status s = LFS_SUCC;
//    pthread_rwlock_rdlock(&chunk->rw_lock);
//    FILE* fp = NULL;
//    char chunk_name[512];
//#if __WORDSIZE == 64
//    sprintf(chunk_name, "%s/%s%ld", chunk->path, CHUNK_PREFIX, chunk->id);
//#else
//    sprintf(chunk_name, "%s/%s%lld", chunk->path, CHUNK_PREFIX, chunk->id);
//#endif
//    if ((fp = fopen(chunk_name, "rb")) == NULL)
//    {
//        logger(FATAL(mLog), "get %s fail, the file may be lost or damage", chunk_name);
//        s = LFS_FAIL;
//    }
//    else
//    {
//        char tmp[10240];
//        //get length
//        fseek(fp, 0, SEEK_END);
//        int32_t len = ftell(fp);
//        fseek(fp, 0, SEEK_SET);
//        int32_t tmp_size = 0;
//        int32_t page = len < 10240 ? len : 10240;
//        while (tmp_size < len)
//        {
//            fread(tmp, page, 1, fp);
//            logger(DEBUG(mLog), "%d byte", page);
//            buffer_write(buf, tmp, page);
//            tmp_size += page;
//            memset(tmp, 0, 10240);
//        }
//    }
//    fclose(fp);
//    pthread_rwlock_unlock(&chunk->rw_lock);
//    return s;
//}
//
//Status chunk_set_all_data(const char* path, int64_t id, void* data, int32_t size)
//{
//    char chunk_name[512];
//#if __WORDSIZE == 64
//    sprintf(chunk_name, "%s/%s%ld", path, CHUNK_PREFIX, id);
//#else
//    sprintf(chunk_name, "%s/%s%lld", path, CHUNK_PREFIX, id);
//#endif
//    if (io_file_exist(chunk_name))
//    {
//        logger(FATAL(mLog), "chunk '%s' is exist,can not create the same name chunk", chunk_name);
//        return LFS_CS_CHUNK_HAD_EXIST;
//    }
//    FILE *fp = NULL;
//    if ((fp = fopen(chunk_name, "wb")) == NULL)
//    {
//        logger(FATAL(mLog), "can not create the file [%s]", chunk_name);
//        return LFS_FAIL;
//    }
//    fwrite(data, size, 1, fp);
//    fclose(fp);
//    return LFS_SUCC;
//}
